function addContact() {
    socket.emit("addContact", getCookie('profile'));

    socket.on("contactAdded", function() {
        alert("Contact added.");
    });
}

socket.on("contactRemoved", function () {
    alert("Contact removed.");
    location.reload();
});

function swapView(view) { // swapView function from chat.js, profile version. Simply add elements in beginning and make if statements for additional items.

    document.getElementById('profile-best-moments-container').style.display = "none";
    document.getElementById('profile-best-moments-header').style.display = "none";
    document.getElementById('profile-your-contacts-header').style.display = "none";
    document.getElementById('profile-your-contacts-container').style.display = "none";
    if (view === 'bestMoments') {
        document.getElementById('profile-best-moments-container').style.display = "block";
        document.getElementById('profile-best-moments-header').style.display = "block";
    }
    else if (view === 'following') {
        document.getElementById('profile-your-contacts-header').style.display = "block";
        document.getElementById('profile-your-contacts-container').style.display = "block";
    }

    //document.getElementById('followingButton').style.color = "#e7e7e7";
    //document.getElementById('bestMomentsButton').style.color = "#e7e7e7";

    // document.getElementById(view + 'Button').style.color = "#D34836";
}

function getProfile(callback) {

    if (typeof (window.location.href.split('=')[1]) !== 'undefined' && (window.location.href.split('=')[1]).indexOf('#') === -1) {

        document.cookie = "profile=; expires=Thu, 01 Jan 1970 00:00:00 GMT";

        var d = new Date(),
                daysToExist = 30;

        d.setTime(d.getTime() + (daysToExist * 24 * 60 * 60 * 1000));

        document.cookie = "profile=" + window.location.href.split('=')[1] + "; expires=" + d.toGMTString();
    }

    socket.emit("getProfile", getCookie('profile'));

    socket.on("profileData", function(data) {
        if (data.bool) {
            if (data.contacts.length > 0) {
                data.userContacts = "";
                data.contacts.forEach(function(contact) {
                    data.userContacts += '<div class="profile-contact"><div class="profile-contact-container"><div class="profile-contact-image"></div><h3 class="profile">' + contact + '</h3><div class="profile-contact-delete"></div></div><div class="profile-contact-invite-to-chat"><h3>CHAT</h3></div></div>';
                });
            } else {
                data.userContacts = '<h3 class="profile" style="margin: 30px 0px 0px 30px;">' + data.user + ' does not follow anyone yet.</h3>';
            }

            if (data.topMsgs === "empty") {
                data.msgs = '<h3 class="profile" style="margin: 30px 0px 0px 30px;">' + data.user + ' has no best moments yet.</h3>';
            } else {
                data.msgs = "";
                data.topMsgs.forEach(function(msgObject) {
                    if (msgObject.votes > 0) {
                        data.msgs += '<span class="1' + msgObject.chatID + '">\n\
                                <div id="replyDiv' + msgObject.msgID + '" class="replyDivs">Hej de e ja som e replyDiv</div><div id="' + msgObject.msgID + '" class="message-container" style="background-color: #D221E6">\n\
                                <div class="message-author">chat: ' + msgObject.chatName + '</div>\n\
                                <div class="message-timestamp">+' + msgObject.votes + '</div><img style="display:none" id="replyIndicator' + msgObject.msgID + '" src="bilder/reply.png" onmouseover="showReply(' + msgObject.msgID + ');" onmouseout="hideReply(' + msgObject.msgID + ');"></img>\n\
                                <div class="message-image" style="background-image:url(bilder/tiger.png);">\n\
                                <div class="message-account"></div></div>\n\
                                <div class="message-overlay"></div><div class="chatMsg">' + msgObject.msg + '</div></div></span>';
                    }
                });
            }

            callback(data);

            //data.user data.email data.authlevel data.totalvotes
        } else {
            var resObj = new Object();
            resObj.user = 'User does not exist';
            resObj.email = 'non@existent.user';
            resObj.authlevel = 'Pile of ashes';
            resObj.gender = "Groovy";
            resObj.age = "Old enough";
            resObj.description = "Hello, you have tried to view the profile of a user who does not exist and instead you have found this profile, created by the 'crew'. Have a nice day!";
            resObj.location = "Limbo";
            resObj.contacts = false;
            resObj.topMsgs = "";

            callback(resObj);
        }
    });
}

$(document).on("click", ".profile-contact-delete", function () {
    socket.emit("removeContact", $(this).siblings(".profile").html());
});

socket.on("tokenValidated", function() {
    getProfile(function(data) {
        $('#userName').html(data.user);
        $('#emailProfile').append(data.email);
        $('#authlevelProfile').append(data.authlevel);
        $("#gender").html(data.gender);
        $("#age").html(data.age);
        $("#description").html(data.description);
        $("#location").html(data.location);
        $("#profile-best-moments-container").append(data.msgs);
        $("#profile-your-contacts-container").append(data.userContacts);

        if (data.updateButton)
            $("#profile-edit").css("display", "block");

        document.title = "Profile: " + data.user;
    });
});

function updateProfile(data) {
    socket.emit("updateProfile", data);

    socket.on("profileUpdated", function() {
        location.reload();
    });


}


$(document).on("submit", ".profile-change-info-form", function() {
    var updateObject = {},
            i = 0;

    if ($("#description-profile").val() !== "") {
        updateObject.description = $("#description-profile").val();
        $("#description-profile").val("");
    } else {
        i += 1;
        updateObject.description = $("#description").html();
    }
    if ($("#updateFrom").val() !== "") {
        updateObject.from = $("#updateFrom").val();
        $("#updateFrom").val("");
    } else {
        i += 1;
        updateObject.from = $("#location").html();
    }
    if ($("#updateGender").val() !== "") {
        updateObject.gender = $("#updateGender").val();
        $("#updateGender").val("");
    } else {
        i += 1;
        updateObject.gender = $("#gender").html();
    }
    if ($("#updateAge").val() !== "") {
        updateObject.age = $("#updateAge").val();
        $("#updateAge").val("");
    } else {
        i += 1;
        updateObject.age = $("#age").html();
    }

    if (i < 4)
        updateProfile(updateObject);

    return false;
});